﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;

namespace P03
{
    public class A08 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P03A08 P = (P03A08)handler.SerialApi.Deserialize(typeof(P03A08), parameter, CR.SerialType);

            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
                sps[0].Value=P.FID;

                String sql = "Select D.FID BagJetFID,IsNull(D.FDecimal,0) BagJetCurrent,IsNull(D.FDecimal1,0) BagJetFix,D.FInteger BagJetCurrentNum FROM KEW_SalesInfo A LEFT JOIN KEW_ProductsEntry B ON A.FBase=B.FID LEFT JOIN KEW_ProductsBagRuleEntry C ON B.FBase1=C.FID LEFT JOIN KEW_SalesBathManagerEntry D ON D.FBASE=B.FID WHERE A.FID=@FID";
                Recordset rs = handler.Data.ExeProcedure(P.AccountNo, sql, sps);

                if (rs == null || rs.Data.Tables.Count <= 0 || rs.Data.Tables[0].Rows.Count <= 0)
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "产品批次信息不存在";
                    return;
                }

                SalesBatch temp = new SalesBatch();
                foreach (DataRow row in rs.Data.Tables[0].Rows)
                {
                    temp.BagJetCurrent = Common.CDbl(row["BagJetCurrent"].ToString());//当前数量
                    temp.BagJetCurrentNum = Common.CInt(row["BagJetCurrentNum"].ToString());//当前编号
                    temp.BagJetFix = Common.CDbl(row["BagJetFix"].ToString());//批次数量
                    temp.BagJetFID = Common.CInt(row["BagJetFID"].ToString());//当前编号

                }

                if (temp.BagJetCurrent+P.Qty>=temp.BagJetFix)
                {
                    temp.BagJetCurrent = temp.BagJetCurrent + P.Qty - temp.BagJetFix;
                    temp.BagJetCurrentNum++;
                }
                else
                {
                    temp.BagJetCurrent = temp.BagJetCurrent + P.Qty ;
                }

                SqlParameter[] sps1 = new SqlParameter[3];
                sps1[0] = new SqlParameter("@FID", SqlDbType.Int);
                sps1[1] = new SqlParameter("@FInteger", SqlDbType.Int);
                sps1[2] = new SqlParameter("@FDecimal", SqlDbType.Decimal);
                sps1[0].Value=temp.BagJetFID;
                sps1[1].Value=temp.BagJetCurrentNum;
                sps1[2].Value=temp.BagJetCurrent;

                sql = "UPDATE KEW_SalesBathManagerEntry SET FDecimal=@FDecimal,FInteger=@FInteger WHERE FID=@FID";

                rs = handler.Data.ExeProcedure(P.AccountNo, sql, sps1);

                if (rs.Enable)
                {
                    CR.Result = EnumResult.Normal;
                    CR.Message = "产品批次信息记录成功";
                    handler.Message(MessageType.Note, String.Format("操作员{0}对单据{1}进行了产品批次信息记录", user.UserName, P.FID));
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = rs.Message;

                }

                handler.Message(MessageType.Note, CR.Message);
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = ex.Message;
                handler.Message(MessageType.Error, String.Format("发生错误:{0}", ex.Message));
            }
        }
    }

    public class SalesBatch
    {
        public double BagJetCurrent;
        public double BagJetFix;
        public int BagJetCurrentNum=0;
        public int BagJetFID;
    }

    public class P03A08
    {
        public String FID = "";
        public String AccountNo;
        public double Qty;
    }
}
